<?php
use  yii\helpers\Html;
use kartik\file\FileInput;
?>

<div class="item" style="border:1px solid #CCC;border-radius:3px;padding:15px;margin-bottom:15px;">
    <div class="label label-primary">&nbsp;<?= $val['key']+1 ?>&nbsp;</div>
    <div class="form-group">
        <label class="control-label col-sm-2" for="product-name">行程标题</label>
        <div class="col-sm-5">
            <?= Html::textInput("Product[trip][$i][title]", $val['title'], ['class'=>'form-control']) ?>
        </div>
    </div>

    <div class="form-group">
        <label class="control-label col-sm-2" for="product-name">餐饮</label>
        <div class="col-sm-4">
            <?= Html::textInput("Product[trip][$i][eat]", $val['eat'], ['class'=>'form-control']) ?>
        </div>

        <label class="control-label col-sm-1 " for="product-name">住宿</label>
        <div class="col-sm-4">
            <?= Html::textInput("Product[trip][$i][room]", $val['room'], ['class'=>'form-control']) ?>
        </div>
    </div>

    <div class="form-group">
        <label class="control-label col-sm-2" for="product-name">交通</label>
        <div class="col-sm-4">
            <?php if(isset($val['traffic'])){ ?>
            <?= Html::textInput("Product[trip][$i][traffic]", $val['traffic'], ['class'=>'form-control']) ?>
            <?php }else{ ?>
             <?= Html::textInput("Product[trip][$i][traffic]",'', ['class'=>'form-control']) ?>
            <?php }?>   
        </div>
    </div>
    <div class="form-group">
        <label class="control-label col-sm-2" for="product-name">抵达</label>
        <div class="col-sm-4">
            <?= Html::textInput("Product[trip][$i][time1]", $val['time1'], ['class'=>'form-control']) ?>
        </div>

        <label class="control-label col-sm-1 " for="product-name">起航</label>
        <div class="col-sm-4">
            <?= Html::textInput("Product[trip][$i][time2]", $val['time2'], ['class'=>'form-control']) ?>
        </div>
    </div>


    <div class="form-group">
        <label class="control-label col-sm-2" for="product-name">途经国家</label>
        <div class="col-sm-4">
            <?= Html::textInput("Product[trip][$i][country]", $val['country'], ['class'=>'form-control']) ?>
        </div>

        <label class="control-label col-sm-1 " for="product-name">类型</label>
        <div class="col-sm-4">
            <?= Html::dropDownList("Product[trip][$i][type]", $val['type'], Yii::$app->params['trip_type'], ['class'=>'form-control']) ?>
        </div>
    </div>

    <div class="form-group">
        <label class="control-label col-sm-2" for="product-name">行程图片</label>
        <div class="col-sm-9">
            <?php
            $arr = $arr_config = [];
            if(!empty($trip_img)){
                $trip_img = json_decode($trip_img);
                foreach($trip_img as $kt => $vt){
                    if($kt == $i){
                        foreach($vt as $k=>$v){
                            $image_path = '/'.$v;
                            $arr[] =  Html::img($image_path,  ['class'=>'file-preiew-image', 'width'=>'200' ]);
                            $arr_config[]=['url'=>sprintf("/product/product/del-img?id=%d&img_id=%s&key=%d",$id,$k,$i)];
                        }
                    }
                }    
            }
            echo FileInput::widget([
                           'language' => 'zh',
                           'name' => 'Product[trip_img]',
                           //'model' => $img,
                           //'id' => 'file-upload',
                           'options' => ['accept' => '', 'multiple'=>true],
                           'pluginOptions' => [
                                'showUpload'  => false,
                                'initialPreview'=> $arr,
                                'initialPreviewConfig' => $arr_config,
                                'showRemove'  => false,
                                'uploadAsync'=> true,
                                'uploadUrl' => \yii\helpers\Url::to(['/product/product/file-upload','key'=>$i,'id'=>$id]),
                                'maxFileSize' => 8120,
                                'maxFileCount' => 20,
                           ]
            ]); ?>
        </div>
    </div>

    <div class="form-group">
        <label class="control-label col-sm-2" for="product-name">行程描述</label>
        <div class="col-sm-9">
            <?= Html::textArea("Product[trip][$i][desc]", $val['desc'], ['class'=>'form-control', 'rows'=>4]) ?>
        </div>
    </div>

    <div class="form-group">
        <label class="control-label col-sm-2" for="product-name">岸上游览</label>
        <div class="col-sm-9" style="border:1px solid #ddd;margin:15px;padding-top: 15px;border-radius: 3px;padding-right: 15px;">
            <?php if(isset($val['visit']) && !empty($val['visit'])){ ?>
                <?php $nums=0;?>  
                <?php foreach($val['visit'] as $k=>$v){ ?>
                <?php $nums++?>
                <div class="form-group visit-item-<?=$i?>" data-key="<?=$k?>" style="margin-right: 0;">
                    <label class="control-label col-sm-1" for="product-name">标题</label>
                    <div class="col-sm-2">
                    <?= Html::textInput("Product[trip][$i][visit][$k][name]", $v['name'], ['class'=>'form-control']) ?>
                    </div>
                    <label class="control-label col-sm-1" for="product-name">价格</label>
                    <div class="col-sm-2">
                    <?= Html::textInput("Product[trip][$i][visit][$k][price]",isset($v['price'])?$v['price']:'', ['class'=>'form-control']) ?>
                    </div>
                    <label class="control-label col-sm-1" for="product-name">描述</label>
                    <div class="col-sm-4">
                    <?= Html::textArea("Product[trip][$i][visit][$k][content]", $v['content'], ['class'=>'form-control', 'rows'=>4]) ?>
                    </div>
                    <div class="col-sm-1">
                        <?php if($nums == 1){ ?>
                        <a class="btn btn-default visit-add" data-item="<?=$i?>">添加</a>
                        <?php }else{ ?>
                        <a class="btn btn-danger visit-remove" data-item="<?=$i?>">删除</a>
                        <?php };?>
                    </div>
                </div>    
                <?php }?>
            <?php }else{ ?>

                <div class="form-group visit-item-<?=$i?>" data-key="0" style="margin-right: 0;">
                    <label class="control-label col-sm-1" for="product-name">标题</label>
                    <div class="col-sm-2">
                    <?= Html::textInput("Product[trip][$i][visit][0][name]",'', ['class'=>'form-control']) ?>
                    </div>
                    <label class="control-label col-sm-1" for="product-name">价格</label>
                    <div class="col-sm-2">
                    <?= Html::textInput("Product[trip][$i][visit][0][price]",'', ['class'=>'form-control']) ?>
                    </div>
                    <label class="control-label col-sm-1" for="product-name">描述</label>
                    <div class="col-sm-4">
                    <?= Html::textArea("Product[trip][$i][visit][0][content]",'', ['class'=>'form-control', 'rows'=>4]) ?>
                    </div>
                    <div class="col-sm-1">
                        <a class="btn btn-default visit-add" data-item="<?=$i?>">添加</a>
                    </div>
                </div>
                   
               
            <?php }?>    
        </div>
    </div>

</div>

<?php app\components\JsBlock::begin() ?>
<script type="text/javascript">

    $(".visit-add").click(function(){ 
        var keys_arr = [];
        var data_item = $(this).attr("data-item"); 
        var item_class = ".visit-item-"+data_item;
        $(item_class).each(function(i){
            keys_arr[i] = $(this).attr('data-key');
            i++;
        });
        var key = Math.max.apply(null,keys_arr)+1;
        var html = '<div class="form-group visit-item-'+data_item+'" data-key='+key+' style="margin-right: 0;">'+
                    '<label class="control-label col-sm-1" for="product-name">标题</label>'+
                    '<div class="col-sm-2">'+
                    '<input type="text" class="form-control" name="Product[trip]['+data_item+'][visit]['+key+'][name]" value=""></div>'+
                    '<label class="control-label col-sm-1" for="product-name">价格</label>'+
                    '<div class="col-sm-2">'+
                    '<input type="text" class="form-control" name="Product[trip]['+data_item+'][visit]['+key+'][price]" value=""></div>'+
                    '<label class="control-label col-sm-1" for="product-name">描述</label>'+
                    '<div class="col-sm-4">'+
                    '<textarea class="form-control" name="Product[trip]['+data_item+'][visit]['+key+'][content]" rows="4"></textarea></div>'+
                    '<div class="col-sm-1"><a class="btn btn-danger visit-remove" data-item='+data_item+'>删除</a></div>'+
                    '</div>';

        $(item_class).last().after(html);
   });

   $("div").delegate(".visit-remove","click",function(){
        var data_item = $(this).attr("data-item"); 
        var item_class = ".visit-item-"+data_item;
        $(this).parents(item_class).remove();
    });    

</script>
<?php app\components\JsBlock::end() ?>
